/**
 Redistribution and use in source and binary forms, with or
 without modification, are permitted provided that the following conditions are met:

 * Redistributions of source code must retain the above copyright notice, this
 list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 notice, this list of conditions and the following disclaimer
 in the documentation and/or other materials provided with the distribution.
 * Neither the names of the authers, nor the names of its contributors, may be used to endorse or 
 promote products derived from this software without specific prior written permission.

 THIS SOFTWARE IS PROVIDED BY THE REGENTS/COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND 
 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
 REGENTS/COPYRIGHT OWNERS AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 @Author Fredrique Samuels <fredriquesamuels@gmail.com>
 */

#include <string>
#include "transport.h"

namespace tri{
namespace transport{


//=======================================================

void write(std::string& s ,void * c,int size){
    std::string t;
    t.assign ((char*)c, size);
    s+=t;
}

//=======================================================

void write(std::string& s,int& t){
    write(s,&t,sizeof(int));
}

//=======================================================

void write(std::string& s,float& t){
    write(s,&t,sizeof(float));
}

//=======================================================

void write(std::string& s,long& t){
    write(s,&t,sizeof(long));
}

//=======================================================

void write(std::string& s,double& t){
    write(s,&t,sizeof(double));
}

//=======================================================

void write(std::string& s,timestamp& t){
    time_t _t = t.get(); 
    write(s,&_t,sizeof(time_t));
}

//=======================================================


void write(std::string& s,std::string& t){
    int size = (int)t.size();
    write(s,&size,sizeof(int));
    for(int i = 0;i<size;i++){
        s+=t.c_str()[i];
    }
    
}

//=======================================================

void read(std::string& s,int& index,float &v){
    v = *((float*)(&s.c_str()[index]));
    index+=sizeof(float);    
}

//=======================================================

void read(std::string& s,int& index,int &v){
    v = *((int*)(&s.c_str()[index]));
    index+=sizeof(int);    
}

//=======================================================

void read(std::string& s,int& index,double &v){
    v = *((double*)(&s.c_str()[index]));
    index+=sizeof(double);    
}

//=======================================================

void read(std::string& s,int& index,long &v){
    v = *((long*)(&s.c_str()[index]));
    index+=sizeof(long);    
}

void read(std::string& s,int& index,std::string& v,int size){
    v = s.substr(index,index+size);
    index+=sizeof(char)*size;
}



//=======================================================

void read(std::string& s,int& index,timestamp &v){
    time_t t = *((time_t*)(&s.c_str()[index]));
    index+=sizeof(time_t);    
    v = timestamp(t);
}

//=======================================================

void read(std::string& s,int& index,std::string &v){
    int size;
    read(s,index,size);
    v = "";    
    for(int i = 0;i<size;i++){
        v+=s.c_str()[index+i];
    }
    index+=sizeof(char)*size;
}

//=======================================================

void write_type(std::string& s,int& t){int i = TRANSPORT_INT;write<int>(s,i);}
void write_type(std::string& s,float& t){int i = TRANSPORT_FLOAT;write<int>(s,i);}
void write_type(std::string& s,long& t){int i = TRANSPORT_LONG;write<int>(s,i);}
void write_type(std::string& s,double& t){int i = TRANSPORT_DOUBLE;write<int>(s,i);}
void write_type(std::string& s,timestamp& t){int i = TRANSPORT_TIMESTAMP;write<int>(s,i);}
void write_type(std::string& s,std::string& t){int i = TRANSPORT_STRING;write<int>(s,i);}


void read_transport_packet_header(std::string& s,int &index,packet_header& h){
    read(s,index,h.header);
    read(s,index,h.packet_type);
    read(s,index,h.channel);
    read(s,index,h.ts);
    read(s,index,h.size);
}

void read_transport_packet_header(std::string& s,packet_header& h){
    int index = 0;
    read(s,index,h.header);
    read(s,index,h.packet_type);
    read(s,index,h.channel);
    read(s,index,h.ts);
    read(s,index,h.size);
}

}
}

std::ostream &operator<<(std::ostream& out,tri::transport::packet_header& h){
    out<<"<";
    out<<h.header<<" ";
    out<<"type="<<h.packet_type<<" ";
    out<<"channel="<<h.channel<<" ";
    out<<"timstamp="<<h.ts<<" ";
    out<<"size="<<h.size<<">";
    return out;
}




